(function ($){
	var Carousel = function(element, options){
		this.$element = $(element);
		this.options = options || Carousel.DEFAULT;
		this.interval = null;
		this.parsed = null;
		this.cycling = null;
		this.$item = null;
	}

	Carousel.DEFAULT = { 
		interval: 5000,
		pause: 'hover'
	}

	Carousel.prototype.cycle = function(){
		// console.log(this.$element);
		var that = this;
		console.log(this.options.interval);
		this.interval = setInterval(function(){
			that.next();
		},this.options.interval);
		console.log(this.interval);
	}

	Carousel.prototype.stopPlay = function(){

	}

	Carousel.prototype.to = function(){

	}

	Carousel.prototype.getItemIndex = function (item) {
		console.log(item);
    this.$items = item.parent().children('[data-defind="item"]');
    console.log(this.$items.index(item || this.$active));
    return this.$items.index(item || this.$active);
  }

  Carousel.prototype.getItemForDirection = function (direction, active) {
    var activeIndex = this.getItemIndex(active);
    var willWrap = (direction == 'prev' && activeIndex === 0)
                || (direction == 'next' && activeIndex == (this.$items.length - 1))
    // if (willWrap && !this.options.wrap) return active
    var delta = direction == 'prev' ? -1 : 1;
  	console.log(delta);
    var itemIndex = (activeIndex + delta) % this.$items.length;
    console.log(itemIndex);
    return this.$items.eq(itemIndex)
  }

	Carousel.prototype.pause = function(){
		this.interval = clearInterval(this.interval);
		return this;
	}

	Carousel.prototype.next = function(){
		this.slide();
	}

	Carousel.prototype.slide = function(){
		
		console.log(this);
		var $active = this.$element.find('.active');
		console.log($active);
		var $next = this.getItemForDirection("next",$active);
		console.log($next);

		this.pause();
		// var $next = this.getItemForDirection($active);
		$active.removeClass("active");
		$next.addClass("active");

		this.cycle();	
	}


	function Plugin(option){
		this.each(function(){
			//console.log(this);
			var $this = $(this);
			data = new Carousel(this, option);
			if(typeof option == 'number') data.to(option);
			else {
				data.pause().cycle();
			}
		});
	}

	$(document).on("click.carousel", '[data-fn="carousel"]', function(){
		console.log("1");
		console.log(this);
		var $this = $(this);
		console.log($this.attr("data-target"));
	});

	$(window).on('load',function(){
		$('[data-ride="carousel"]').each(function(){
			var $carousel = $(this);
			// console.log($carousel);
			Plugin.call($carousel);
		});
	});

}(jQuery));